<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
    <el-form-item label="系统类型">
      <el-radio-group v-model="dataForm.osType">
        <el-radio label="">不限</el-radio>
        <el-radio label="android">安卓</el-radio>
        <el-radio label="ios">苹果</el-radio>
        <el-radio label="mini">小程序</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="版本号">
      <el-input v-model="dataForm.versionCode" placeholder="版本号" style="width: 300px;"></el-input>
    </el-form-item>
    <el-form-item label="key" prop="key">
      <el-input v-model="dataForm.key" placeholder="key" style="width: 300px;" :disabled="isWrite"></el-input>
      <el-button type="primary" @click="changeKey" v-if="dataForm.id">输入验证码修改</el-button>
    </el-form-item>
    <el-form-item label="value输入方式">
      <el-radio-group v-model="inputType">
        <el-radio :label="0">字符串</el-radio>
        <el-radio :label="1">上传文件</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="value" prop="value" v-if="inputType==0">
      <el-input v-model="dataForm.value" placeholder="value"></el-input>
    </el-form-item>
    <el-form-item label="value" prop="value" v-if="inputType==1">
      <el-upload
        ref="uploadBannerImg"
        :action="upload"
        :show-file-list="true"
        :file-list="fileList"
        :on-success="handleSuccess"
        :on-error="handleError"
        :before-upload="beforeUpload">
        <el-button type="primary" size="mini">上传</el-button>
      </el-upload>
    </el-form-item>
    <el-form-item label="备注" prop="remark">
      <el-input v-model="dataForm.remark" type="textarea" rows="2" placeholder="备注"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        keyCheck: 'xxx',
        isWrite: false,
        inputType: 0,
        fileList: [],
        upload: this.$http.adornUrl(`/sys/oss/upload?token=${this.$cookie.get('token')}`),
        dataForm: {
          key: '',
          url: '',
          remark: '',
          osType: '',
          versionCode:''
        },
        dataRule: {
          value: [
            { required: true, message: 'value不能为空', trigger: 'blur' }
          ],
          key: [
            { required: true, message: '广告页面key不能为空', trigger: 'blur' }
          ],
          remark: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.inputType = 0
        this.fileList = []
        if (this.dataForm.id) {
          this.isWrite = true
        }
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/picbook/adconf/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.data.adConf
              }
            })
          }
        })
      },
      handleSuccess (res, file) {
        this.dataForm.value = res.url
        this.fileList = [{'name':res.url}]
      },
      handleError (err, file, fileList) {
        this.$message.error('上传失败')
      },
      beforeUpload (file) {
        const isLt2M = file.size / 1024 / 1024 < 10;
        if (!isLt2M) {
          this.$message.error('上传头像图片大小不能超过 10MB!');
        }
      },
      changeKey () {
        this.$prompt('请输入验证码', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({ value }) => {
          if (value == this.keyCheck) {
            this.isWrite = false
            this.$message({
              type: 'success',
              message: '验证正确'
            });
          } else {
            this.$message({
              type: 'error',
              message: '验证错误'
            }); 
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/picbook/adconf/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                adConf: this.dataForm
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  // duration: 1500,
                  duration: 500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
